package cn.iocoder.yudao.module.shop.controller.app.order;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.shop.controller.admin.item.vo.ShopItemRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.order.vo.ShopOrderPageReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.order.vo.ShopOrderRespVO;
import cn.iocoder.yudao.module.shop.convert.order.ShopOrderConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.order.ShopOrderDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.order.ShopOrderItemDO;
import cn.iocoder.yudao.module.shop.service.item.ShopItemService;
import cn.iocoder.yudao.module.shop.service.order.ShopOrderItemService;
import cn.iocoder.yudao.module.shop.service.order.ShopOrderService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "用户 App - 礼品卡")
@RestController
@RequestMapping("/shop/order")
@RequiredArgsConstructor
@Validated
@Slf4j
public class AppShopOrderController {

    @Resource
    private ShopOrderService orderService;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private ShopOrderItemService shopOrderItemService;

    @Resource
    private ShopItemService shopItemService;


    @GetMapping("/page")
    @Operation(summary = "获得店内消耗订单分页")
    public CommonResult<PageResult<ShopOrderRespVO>> getOrderPage(@Valid ShopOrderPageReqVO pageVO) {
        PageResult<ShopOrderDO> pageResult = orderService.getOrderPage(pageVO);
        if(CollUtil.isEmpty(pageResult.getList())){
            return success(PageResult.empty());
        }

        // 查询艾灸师
        Set<Long> userIds = convertSet(pageResult.getList(), ShopOrderDO::getUserId);
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), ShopOrderDO::getMemberId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        List<ShopOrderItemDO> shopOrderItemDOS = shopOrderItemService.getShopOrderItemListByOrderId(convertSet(pageResult.getList(), ShopOrderDO::getId));

        //查询店内项目
        Set<Long> itemIds = convertSet(shopOrderItemDOS, ShopOrderItemDO::getOptItemId);
        Map<Long, ShopItemRespVO> shopItemRespVOMap = shopItemService.getItemMap(itemIds);

        return success(ShopOrderConvert.INSTANCE.convertPage(pageResult, shopOrderItemDOS, shopItemRespVOMap, memberMap, userMap));
    }
}
